
package com.tbvtc.agriculturalcreditchainserver2025.comment.redis;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.tbvtc.agriculturalcreditchainserver2025.vo.Cart.CartSelectUserByUserId;
import com.tbvtc.agriculturalcreditchainserver2025.vo.farmerVo.FoodReportVo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyspaceEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.List;

@Configuration
public class RedisConfig {
//    @Bean("TredisTemplate")
//    public RedisTemplate<String, Object> TredisTemplate(RedisConnectionFactory connectionFactory) {
//        RedisTemplate<String, Object> template = new RedisTemplate<>();
//        template.setConnectionFactory(connectionFactory);
//        template.setKeySerializer(new StringRedisSerializer());
//
//        // 配置 ObjectMapper
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.registerModule(new JavaTimeModule());
//        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//
//        // 使用 GenericJackson2JsonRedisSerializer
//        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(objectMapper);
//
//        template.setValueSerializer(serializer);
//        template.setHashValueSerializer(serializer);
//        template.afterPropertiesSet();
//        return template;
//    }
    @Bean("redisTemplate1")
    public RedisTemplate<String, Object> redisTemplate1(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        // Key 序列化器：StringRedisSerializer（避免二进制存储）
        template.setKeySerializer(new StringRedisSerializer());
        // Value 序列化器：JSON 格式（需 Jackson 依赖）
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // Hash Key 序列化器
        template.setHashKeySerializer(new StringRedisSerializer());
        // Hash Value 序列化器
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 初始化参数
        template.afterPropertiesSet();
        return template;
    }
    @Bean("stringObjectRedisTemplate")
    public RedisTemplate<String, Object> stringObjectRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>(); // 创建RedisTemplate对象
        template.setConnectionFactory(redisConnectionFactory); // 设置RedisConnectionFactory
        template.setKeySerializer(RedisSerializer.string()); // 设置key的序列化器
        template.setValueSerializer(RedisSerializer.java()); //  设置value的序列化器
        return template;
    }
    @Bean("cartRedisTemplate")
    public RedisTemplate<String, CartSelectUserByUserId> cartRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, CartSelectUserByUserId> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 定制序列化规则
        Jackson2JsonRedisSerializer<CartSelectUserByUserId> serializer =
                new Jackson2JsonRedisSerializer<>(CartSelectUserByUserId.class);

        // 配置ObjectMapper
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());  // 支持Java8时间类型
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // 禁用时间戳格式
        objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator()); // 保留类型信息

        serializer.setObjectMapper(objectMapper);

        // Key采用String序列化
        template.setKeySerializer(new StringRedisSerializer());
        // HashKey采用String序列化
        template.setHashKeySerializer(new StringRedisSerializer());
        // Value采用JSON序列化
        template.setValueSerializer(serializer);
        // HashValue采用JSON序列化
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }




}

